package com.qingmu.club.minaapi.controller.member;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qingmu.club.minaapi.custom.logic.PushMessageLogic;
import com.qingmu.club.minaapi.plus.entity.*;
import com.qingmu.club.minaapi.custom.logic.EvaluationLogic;
import com.qingmu.club.minaapi.plus.service.*;
import com.qingmu.club.minaapi.utils.ConvertUtil;
import com.qingmu.club.minaapi.utils.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 评价主表 前端控制器
 * </p>
 *
 * @author duke
 * @since 2019-08-20
 */
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.POST, RequestMethod.GET})
@Api(value = "教练相关(会员端小程序)", description = "教练相关(会员端小程序)")
@RequestMapping("/member/coach")
public class MemberCoachController {

    @Autowired
    EvaluationService evaluationService;

    @Autowired
    ClubStaffService clubStaffService;

    @Autowired
    EvaluationLogic evaluationLogic;

    @Autowired
    ClubUsersService clubUsersService;

    @Autowired
    PushMessageLogic pushMessageLogic;

    @Autowired
    PushMessageService pushMessageService;

    @Autowired
    UsersWxService usersWxService;

    @Autowired
    UsersService usersService;

    @RequestMapping(value = "/queryCoach", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询教练信息")
    public ResponseEntity queryCoach(
            @ApiParam(required = true, value = "教练id") @RequestParam(required = true) String coachId
    ) {
        ClubStaff clubStaff =this.clubStaffService.getById(coachId);
        if(clubStaff==null){
            return ResponseObject.error(551,"教练信息找不到");
        }
        HashMap<String,Object> map = ConvertUtil.objectToMap(clubStaff);
        String evaluationStar= this.evaluationLogic.coachEvaluationStar(coachId);

        map.put("evaluationStar",evaluationStar);

        return ResponseObject.success(map);
    }

    @RequestMapping(value = "/addEvaluation", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "会员对教练评价")
    public ResponseEntity add(
            @ApiParam(required = true, value = "评价内容") @RequestParam(required = true) String evaluationContent,
            @ApiParam(required = true, value = "评价星级(0一颗星 1两颗星 2三颗星 3四颗星 4五颗星)") @RequestParam(required = true) Integer evaluationStar,
            @ApiParam(required = true, value = "教练id") @RequestParam(required = true) String coachId,
            @ApiParam(required = true, value = "会员id") @RequestParam(required = true) String clubUserId,
            @ApiParam(required = true, value = "是否匿名 1-是 0-否") @RequestParam(required = true) Integer isAnonymity,
            @ApiParam(required = false, value = "图片url(以逗号隔开)") @RequestParam(required = false) String evaluationImg
    ) {

        Users users = this.usersService.getById(clubUserId);
        if(users==null){
            return ResponseObject.error(401,"会员信息找不到");
        }

        ClubStaff clubStaff =this.clubStaffService.getById(coachId);
        if(clubStaff==null){
            return ResponseObject.error(551,"教练信息找不到");
        }


        Evaluation evaluation = new Evaluation();
        evaluation.setClubUserId(clubUserId);
        evaluation.setEvaluationContent(evaluationContent);
        evaluation.setEvaluationStar(evaluationStar);
        evaluation.setCoachId(coachId);
        evaluation.setUserName(users.getUserName());
        evaluation.setCoachName(clubStaff.getStaffName());
        evaluation.setIsAnonymity(isAnonymity);
        evaluation.setUserIcon(users.getUserIcon());
        if(!StringUtils.isEmpty(evaluationImg))evaluation.setEvaluationImg(evaluationImg);
        boolean state = this.evaluationService.save(evaluation);
        if(!state){
            return ResponseObject.error(503,"评价失败");
        }
        PushMessage pushMessage = this.pushMessageLogic.queryPushMessageList(coachId);
        if(pushMessage!=null) {
            this.pushMessageLogic.sendTemplate(pushMessage.getOpenId(), pushMessage.getFormId(), evaluationContent, evaluationStar, users.getUserName());
            pushMessage.setIsUse(true);
            this.pushMessageService.updateById(pushMessage);
        }
        return ResponseObject.success();
    }

    @RequestMapping(value = "/getByCoachId", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询会员对教练的评价")
    public ResponseEntity getByCoachId(
            @ApiParam(required = true, value = "教练id") @RequestParam(required = true) String coachId
    ) {
        QueryWrapper<Evaluation> qw = new QueryWrapper<>();
        qw.eq(Evaluation.COACH_ID,coachId).orderByDesc(Evaluation.CREATE_TIME);
        List<Evaluation> evaluationList = this.evaluationService.list(qw);

        return ResponseObject.success(evaluationList);
    }

}

